<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addCom="addFn" :list="showArr"></TodoHeader>
    <TodoMain :list="showArr"></TodoMain>
    <TodoFooter :list="showArr" @changeType="typeFn"
    @delCom="delFn"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  data() {
    return {
      list: [
        { id: 100, name: "吃饭", isDone: true },
        { id: 101, name: "睡觉", isDone: false },
        { id: 102, name: "打豆豆", isDone: true },
      ],
      getSel: "all",
    };
  },
  methods: {
    addFn(name) {
      if (name.trim().length <= 0) {
        alert("必须输入内容");
      } else {
        this.list.push({
          id: this.list[this.list.length - 1].id + 1,
          name: name,
          isDone: false,
        });
      }
    },
    typeFn(str) {
      this.getSel = str;
    },
    delFn() {
      this.list = this.list.filter(item=>!item.isDone)
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  computed: {
    showArr() {
      if (this.getSel === "yes") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list; 
      }
    },
  },
};
</script>